<?php
/*
 * @desc:	网上超市的接口
 * @author:	wg
 * @date:	2012-11-27
 * @copyright:	dld.com
 * 
 * @params:	方法返回的数据格式均为:	data = array(
 * 												'page' => array('pageSize' => xx,'pageCount' => xx),
 * 												'data' = array()
 * 												)
 * 相应的商城API代码在:商城目录/dld/
 * 
 */
class SuperMarketApi 
{
	private $_marketHost = '';
	private $_sKey = '4b0b06e2966ec27815f83ebdaa195aa9';
	
	
	public function __construct()
	{
		$this->connect();
	}
	
	private function connect()
	{
		$marketHost = Yii::app()->params['superMarketUrl'];
		$this->_marketHost = $marketHost;
		return true;
		/*
		$res = @get_headers($marketHost);
		if($res)
		{
			$this->_marketHost = $marketHost;
			return TRUE;
		}
		else
		{
			Throw new Exception('Connecting to market.dld.com Fail!',404);
		}
		*/
	}
	
	/**
	 * 同步帐户到商城
	 * params:	$other = array('msn', 'qq', 'office_phone', 'home_phone', 'mobile_phone');
	 *          $flag = 0 | 1     1代表主站注册   0 代表商城注册    功能：避免用户注册时重复添加积分  add by  yt 2012-08-09
	 */
	public function syncUser($username,$password,$email,$other = array(),$flag = 0)
	{
		for($i = 0;$i < 3;$i++)
		{
			try{
			$url = $this->_marketHost . '/dld/act.php?act=adduser';
			$data['flag'] = $flag;
			$data['user'] = $username;
			$data['password'] = $password;
			$data['email'] = $email;
			$data['other'] = serialize($other);
			$res = $this->get($url,$data);
			break;
			}catch(Exception $e)
			{
				sleep(1);
				continue;
			}
		}
		return $res;
	}
	
	/**
	 * 检查该同步是否已经在商城存在,不存在便同步
	 */
	public function checkUser($username,$password,$email,$other = array(),$flag=0)
	{
		$url = $this->_marketHost . '/dld/act.php?act=checkuser';
		$data['flag'] = $flag;
		$data['user'] = $username;
		$data['password'] = $password;
		$data['email'] = $email;
		$data['other'] = serialize($other);
		$res = $this->get($url,$data);
		return $res;
	}
	
	/**
	 * 同步积分 ( 用于用户在 主站上发表评论,分享,上传图片，绑定.....积分同步到商城 )
	 */
	public function synScore($username,$flag = 101)
	{
		$url = $this->_marketHost . '/dld/act.php?act=synScore';
		$data['user'] = $username;
		$data['flag'] = $flag;
		$res = $this->get($url,$data);
		return $res;
	}
	/**
	 * 升级用户(普通用户=>店连店会员卡用户)
	 */
	public function upgradeRank($username)
	{
		$url = $this->_marketHost . '/dld/act.php?act=updateRank';
		$data['user'] = $username;
		$res = $this->get($url,$data);
		return $res;
	}
	
	/**
	 * 更新用户信息
	 */
	public function updateProfile($username,$profile = array())
	{
		if(!count($profile))
		{
			return FALSE;
		}
		$url = $this->_marketHost . '/dld/act.php?act=updateProfile';
		return $this->get($url,$profile);
	}
	
	/**
	 * 获得超市广告推荐广告位中广告及促销商品
	 */
	public function getGoodsAndAdsInfo($goods =array()){
		$url = $this->_marketHost.'/dld/act.php?act=promote_info';
		return $this->get($url,$goods);
	}
	/**
	 * 取得url的数据
	 */
	private function get($url,$params = array())
	{
		$p=trim($this->deData($params));
		$src = $url;
		$p!='' && $src.='&' . $p;
		$client=new HttpClientD($src);
		$client->open();
		$data=@$client->excute();
		//$data = @file_get_contents($src);
		return json_decode($data);
	}
	
	
	/**
	 * 加密数据
	 */
	private function deData($data,$key = '')
	{
		if(empty($key))
		{
			$key = $this->_sKey;
		}
		$nData = array();
		foreach($data as $k => $v)
		{
			$nData[$k] = XxteaD::encrypt($v, $key);
		}
		$dataStr = '';
		foreach($nData as $k2 => $v2)
		{
			$dataStr .= $k2 . '=' . $v2 . '&';
		}
		return $dataStr;
	}
	
	
}